import java.util.Scanner;

public class 二分查找 {
    public static void main(String[] args) {
        int[] arr = {7, 23, 79, 81, 103, 127, 131, 147};

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要查找的数字");
        int number = sc.nextInt();
        int result = binarySearch(arr, 131);

        if (result == -1) {
            System.out.println("数字未找到");
        } else {
            System.out.println("数字在数组中的位置为: " + result);
        }
    }

    public static int binarySearch(int[] arr, int number) {
        int min = 0;
        int max = arr.length - 1;

        while (min <= max) {
            int mid = (max + min) / 2;

            if (arr[mid] > number) {
                max = mid - 1;
            } else if (arr[mid] < number) {
                min = mid + 1;
            } else {
                return mid;
            }
        }

        return -1;
    }
}